//
//  UIView+RCDToast.m
//  MyFamilyNet
//
//  Created by  任辰东 on 2018/9/11.
//  Copyright © 2018年 任辰东. All rights reserved.
//

#import "UIView+RCDToast.h"
static char associateLengthKey;
@implementation UIView (RCDToast)
@dynamic hud;

- (MBProgressHUD *) hud{
    return (MBProgressHUD *)objc_getAssociatedObject(self, &associateLengthKey);
}

- (void) setHud:(MBProgressHUD *)hud
{
    objc_setAssociatedObject(self, &associateLengthKey, hud, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)showLoadingHUDWithMessage:(NSString *)message
{
    [self hideLoadingHUD];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    self.hud.label.text = message;
    self.hud.label.font = UIFontSize(15);
    self.hud.contentColor = [UIColor whiteColor];
    self.hud.bezelView.blurEffectStyle = UIBlurEffectStyleDark;
    self.hud.graceTime = 0.618f;
    self.hud.bezelView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
}

- (void)hideLoadingHUD
{
    if (self.hud) {
        [self.hud hideAnimated:YES];
    }
}

- (void)showWarningHUDWithMessage:(NSString *)message completion:(completeAlertAction)completion
{
    [self hideLoadingHUD];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    self.hud.mode = MBProgressHUDModeCustomView;
    self.hud.bezelView.blurEffectStyle = UIBlurEffectStyleDark;
    self.hud.label.text = message;
    self.hud.label.font = UIFontSize(15);
    self.hud.contentColor = [UIColor whiteColor];
    self.hud.customView = [[UIImageView alloc] initWithImage:UIImageWithName(@"toast_alert")];
    self.hud.bezelView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    [self.hud hideAnimated:YES afterDelay:0.618];
    if (completion) {
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^{
            completion();
        });
    }
}

- (void)showCompletionHUDWithMessage:(NSString *)message completion:(completeAlertAction)completion
{
    
    [self hideLoadingHUD];
    
    [self hideLoadingHUD];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    self.hud.mode = MBProgressHUDModeCustomView;
    self.hud.label.text = message;
    self.hud.label.font = UIFontSize(15);
    self.hud.contentColor = [UIColor whiteColor];
    self.hud.customView = [[UIImageView alloc] initWithImage:UIImageWithName(@"toast_completion")];
    self.hud.bezelView.blurEffectStyle = UIBlurEffectStyleDark;
    self.hud.bezelView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    [self.hud hideAnimated:YES afterDelay:0.618];
    if (completion) {
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^{
            completion();
        });
    }
}


- (void)showTextHUDWithMessage:(NSString *)message
{
    [self hideLoadingHUD];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    self.hud.mode = MBProgressHUDModeText;
    self.hud.label.text = message;
    self.hud.label.font = UIFontSize(15);
    self.hud.contentColor = [UIColor whiteColor];
//    self.hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    self.hud.bezelView.blurEffectStyle = UIBlurEffectStyleDark;
    self.hud.bezelView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
//    self.hud.offset = CGPointMake(0,  200*kScreen_Ratio_H);
    [self.hud hideAnimated:YES afterDelay:1];
}

@end
